from common.configReader import ReadConfig
from httprunner import HttpRunner,Config,Step,RunRequest,RunTestCase
from buinese.test_login import TestCaseLogin as Login # 导入登录文件
base_path = ReadConfig("config.ini").get_option('dev_base_url')


class Perform(HttpRunner):
    config = Config("test start..").base_url(base_path).verify(False)
    teststeps = [
        Step(
        RunTestCase("登录").
            call(Login).
            export(*["token",])),
    ]
"""
淘宝APP：
游客
用户
商家
"""
# 灵活替换token权限
def reset_token(token=Login):
    Perform.teststeps.clear()       # 清空列表信息，清除登录信息
    Perform.teststeps.append(
        Step(
        RunTestCase('登录')
            .call(token)
            .export(*['token',])
        )
    )
# 发起所有的请求，都需要依赖token，所以这里写死token读取方式
headers = {'Authori-zation': 'Bearer ${token}'}
# 发起请求
def requests_send(reqeusts_name,request_method,path,request_body=None,headers=headers):
    if request_method == 'post':
        return RunRequest(reqeusts_name)\
            .post(path)\
            .with_json(request_body)\
            .with_headers(**headers)\
            .extract()\
            .with_jmespath("body",'res_data')
    elif request_method == 'get':
        return RunRequest(reqeusts_name)\
            .get(path)\
            .with_headers(**headers)\
            .extract()\
            .with_jmespath("body",'res_data')
    elif request_method == 'put':
        return RunRequest(reqeusts_name) \
            .put(path) \
            .with_json(request_body) \
            .with_headers(**headers) \
            .extract() \
            .with_jmespath("body", 'res_data')
    elif request_method == 'delete':
        return RunRequest(reqeusts_name) \
            .delete(path) \
            .with_headers(**headers) \
            .extract() \
            .with_jmespath("body", 'res_data')

def step_obj(obj):
    return Step(obj)

def get_last_response(obj:list):
    Perform.teststeps.extend(obj)
    p1 = Perform()
    p1.test_start()
    res_data = p1.with_export(['token',"res_data"]).get_export_variables()      # 处理成一个字典的形式返回
    return res_data     # 返回所有的response body

# list1 = []
# list2 = [22,11,223,33]
# list1.extend(list2)
# print(list1)        # [11,223,33,22]


















